package com.xiaoming.backend.controller;

import com.xiaoming.backend.dto.request.TaskRequest;
import com.xiaoming.backend.dto.response.TaskResponse;
import com.xiaoming.backend.entity.orm.User;
import com.xiaoming.backend.result.exception.AppErrorException;
import com.xiaoming.backend.result.exception.error.AppError;
import com.xiaoming.backend.service.ITaskService;
import com.xiaoming.backend.util.JsonUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/task")
@RequiredArgsConstructor
@PreAuthorize("@securityService.checkInstitution(#institutionId)")
@Tag(name = "TaskController", description = "运输单相关接口")
public class TaskController {
    private final ITaskService taskService;

    @PostMapping("/find")
    @Operation(summary = "findTasks", description = "按条件查找运输单")
    public ResponseEntity<?> findTasks(@RequestBody TaskRequest taskRequest) {
        String jsonStr = "";
        try {
            // 从认证信息中提取 User 对象（需类型转换）
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            List<TaskResponse> tasks = taskService.findTasks(user, taskRequest);
            jsonStr = JsonUtil.objToJson(tasks);
        }catch (AppErrorException e){
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getErrorMsg());
        }catch (Exception e){
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(AppError.TOKEN_ERROR.getErrorMsg());
        }
        return ResponseEntity.ok(jsonStr);
    }

    @PostMapping("/start")
    @Operation(summary = "startTasks", description = "开始运输")
    public ResponseEntity<?> startTasks(@RequestBody TaskRequest taskRequest) {
        String jsonStr = "";
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String msg = taskService.startTask(user,taskRequest);
            jsonStr = JsonUtil.objToJson(msg);
        }catch (AppErrorException e){
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getErrorMsg());
        }catch (Exception e){
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(AppError.TOKEN_ERROR.getErrorMsg());
        }
        return ResponseEntity.ok(jsonStr);
    }

    @PostMapping("/finish")
    @Operation(summary = "finishTasks", description = "完成运输")
    public ResponseEntity<?> finishTasks(@RequestBody TaskRequest taskRequest) {
        String jsonStr = "";
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            int i = taskService.finishTask(user, taskRequest);
            jsonStr = JsonUtil.objToJson(i);
        }catch (AppErrorException e){
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getErrorMsg());
        }catch (Exception e){
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(AppError.TOKEN_ERROR.getErrorMsg());
        }
        return ResponseEntity.ok(jsonStr);
    }
}
